package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.hosp.repository.HospitalRepository;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName HospitalServiceImpl
 * @Description: 类描述
 * @Author: admin
 * @CreateDate: 2024/6/3 16:24
 * @UpdateUser: 更新人
 * @UpdateDate: 2024/6/3 16:24
 * @UpdateRemark: 更新的信息
 * @Version: 1.0
 */
@Slf4j
@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> paramsMap) {
        //将map转为对象
        String mapString = JSONObject.toJSONString(paramsMap);
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);
        //判断是否存在该医院
        String hoscode = hospital.getHoscode();
        //判断是否存在该医院，根据id查询
        Hospital hospitalExist=hospitalRepository.getHospitalByHoscode(hoscode);
        //存在修改
        if(hospitalExist != null){
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);

            hospitalRepository.save(hospital);
        }
        //不存存在添加
        else {
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);

            hospitalRepository.save(hospital);
        }

    }

    /**
     * 根据hoscode查询医院信息
     * @param hoscode
     * @return
     */
    @Override
    public Hospital getByHospcode(String hoscode) {
       return hospitalRepository.getHospitalByHoscode(hoscode);
    }

    /**
     * 根据条件查询医院列表
     * @param page
     * @param limit
     * @param hospitalQueryVo
     * @return
     */
    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //设置页面以及每页显示的条数，0才是第一页
        Pageable pageable = PageRequest.of(page-1, limit);

        Hospital hospital = new Hospital();
        //将departmentQueryVo对象转为department对象
        BeanUtils.copyProperties(hospitalQueryVo, hospital);
        hospital.setIsDeleted(0);

        //构建查询条件
        ExampleMatcher matcher = ExampleMatcher.matching()
                //设置为模糊查询
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                //忽略大小写
                .withIgnoreCase(true);
        Example<Hospital> example = Example.of(hospital, matcher);

        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);

        pages.getContent().stream().forEach(item -> {
            this.setHospitalHospType(item);
        });

        return pages;
    }

    /**
     * 更新医院状态
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(String id, Integer status) {
        //根据id查询医院信息
        Hospital hospital = hospitalRepository.findById(id).get();
        //修改状态
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    /**
     * 根据id查询医院信息
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> getHospitalById(String id) {
        //获取医院详细信息，包括地址、医院等级等
        Hospital hospital=setHospitalHospType(hospitalRepository.findById(id).get());

        Map<String, Object> result = new HashMap<>();

        result.put("hospital", hospital);
        //单独处理更直观，预约信息
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);

        return result;
    }

    /**
     * 根据hoscode查询医院信息,模糊查询
     * @param hosname
     * @return
     */
    @Override
    public List<Hospital> selectHospByName(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    /**
     * 根据医院编号获取医院预约挂号的详情
     * @param hoscode
     * @return
     */
    @Override
    public Map<String, Object> item(String hoscode) {
        Map<String, Object> result = new HashMap<>();
        //医院详情
        Hospital hosp = getByHospcode(hoscode);
        Hospital hospital = setHospitalHospType(hosp);
        result.put("hospital", hospital);
        //预约规则
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }

    /**
     * 获取医院详细信息，包括医院等级、地址等
     * @param hospital
     * @return
     */
    private Hospital setHospitalHospType(Hospital hospital){
        //根据dictType和dictValue查询医院等级
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());
        //根据省份编码查询省份、市、区
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());

        hospital.getParam().put("fullAddress", provinceString + cityString + districtString);
        //将医院等级存入param属性中
        hospital.getParam().put("hostypeString", hostypeString);
        return hospital;
    }
}
